package com.engine.dialect;

import com.engine.core.domain.Opt;
import com.engine.core.gen.Mysql;

public interface DialectType {


     boolean isSupport(String dbType);

     static DialectType newInstance(String dbType) {
        return new Mysql();
    }


     default String like(String column,String paramAlias,String funStr,boolean isNot){
         if(isNot){
             if(funStr!=null && funStr.length()>0){
                 return column+" not like %"+funStr+"%";
             }else {
                 return column+" not like %:"+paramAlias+" %";
             }
         }else{
             if(funStr!=null && funStr.length()>0){
                 return column+"  like %"+funStr+"%";
             }else {
                 return column+"  like %:"+paramAlias+" %";
             }
         }

     };

     String leftLike(String column,String value,String funStr,boolean isNot);

     String rightLike(String column,String value,String funStr,boolean isNot);

     String contain(String column,String value,String funStr,boolean isNot);
     String containRight(String column, String paramAlias, String funStr, boolean isNot);

    default String comonOper(String column, Opt opt, String paramAlias, String funStr,boolean containNull, boolean isNot){
            if(funStr!=null && funStr.length()>0){
                if(containNull){
                    return "(("+column+" "+opt.DbOper()+" "+funStr+") or "+trimNullfun(column)+" is null) ";
                }else{
                    return column+" "+opt.DbOper()+" "+funStr;
                }
            }else {
                if(containNull) {
                    return "(("+column + " " + opt.DbOper() + " :" + paramAlias+") or "+trimNullfun(column)+" is null)";
                }else{
                    return column + " " + opt.DbOper() + " :" + paramAlias;
                }
            }
    }


    default String nullIfFun(String val){
        return " nullif("+val+",'') ";
    }

    default String trimFun(String val){
        return " trim("+val+")";
    }


    default String trimNullfun(String column){
         return nullIfFun(trimFun(column));
    }

}
